package b649a01.runners;

import java.io.FileWriter;
import java.io.IOException;
import java.nio.ByteBuffer;

/***
 * 
 * Write a memory dump file with double precision floating point numbers.
 * 
 * @author Chao Sun
 * 
 */
public class memoryDump {
	/**
	 * Constructor.
	 */
	public memoryDump() {
	}

	/**
	 * Dump.
	 * 
	 * @param fileName
	 *          the name of the memory dump file
	 * @throws IOException
	 */
	private void run(String fileName) throws IOException {
		FileWriter writer = new FileWriter(fileName);
		
		ByteBuffer buffer = ByteBuffer.allocate(8);
		buffer.putDouble(50);
		byte input[] = buffer.array();
		char charinput[] = new char[8];
		for (int i = 0; i < 8; i++) {
			charinput[i] = (char) input[i];
		}
		for (int i = 0; i < 131072; i++) {
			writer.write(charinput, 0, 8);
		}
		
		/*ByteBuffer buffer = ByteBuffer.allocate(4);
		buffer.putInt(50);
		byte input[] = buffer.array();
		char charinput[] = new char[4];
		for (int i = 0; i < 4; i++) {
			charinput[i] = (char) input[i];
		}
		for (int i = 0; i < 262144; i++) {
			writer.write(charinput, 0, 4);
		}*/
		
		writer.flush();
	}

	/**
	 * main method for testing.
	 * 
	 * @param args
	 *          the command line string array
	 */
	public static void main(String args[]) {
		if (args.length != 1) {
			System.err.println("Args error! Usage: " + "memoryDump <MEM DUMP FILE>");
			System.exit(1);
		}

		try {
			memoryDump dumper = new memoryDump();
			dumper.run(args[0]);
		} catch (IOException e) {
			System.err.println("Memory dumping error! Exiting ...");
			e.printStackTrace();
		}
	}
}